package com.fzkj.sort;

import java.util.Arrays;

/**
 * 选择排序
 * 通过遍历数组，找出最大/最小值，与指定位置交换数据，重复步骤，直到完成排序。
 * @author yaya
 */
public class SelectionSort {

    public static void main(String[] args) {
        int[] s = {81,94,11,96,12,35,17,95,28,58,41,75,15};
        int[] arr = {9,1,2,5,7,4,8,10,25,11,43,23,6,3,5};
        int[] arr2 = {5,1,4,2,8};
        selectionSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    private static void selectionSort(int[] arr){
        for (int head = 0; head < arr.length; head++) {
            int minIndex = head;
            for (int i = head; i < arr.length; i++) {
                if (arr[i] < arr[minIndex]) {
                    minIndex = i;
                }
            }
            int tmp = arr[minIndex];
            arr[minIndex] = arr[head];
            arr[head] = tmp;
            System.out.println("第 "+ (head + 1) +" 轮选择排序后结果为 -> " + Arrays.toString(arr));
        }
    }
}
